//
//  ForYouRecomdItemCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/18.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "ForYouRecomdItemCell.h"
#import "ForYouRecomdSubCell.h"
#import "ForYouRecommendHeaer.h"
#import "GoodDetailController.h"
@interface ForYouRecomdItemCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@end

@implementation ForYouRecomdItemCell
/*
if (self.likeItems.count % 2 == 0) {
    
    return (RecomGoodsLayoutH * (self.likeItems.count/2)) + (((self.likeItems.count/2) - 1) * 8);
    
} else {
    
    return (RecomGoodsLayoutH * ((self.likeItems.count + 1)/2)) + ((((self.likeItems.count + 1)/2) - 1) * 8);
    
}
 */

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.bottomMargin.constant = 0;
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ForYouRecomdSubCell class]) bundle:nil]forCellWithReuseIdentifier:[ForYouRecomdSubCell identifierStr]];
    
    //注册分区头标题
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ForYouRecommendHeaer class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[ForYouRecommendHeaer identifierStr]];
    
    
    [self.myCollectionView registerClass:[UICollectionReusableView class]
              forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                     withReuseIdentifier:@"CollectionViewFooterView"];
    
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    
    self.myCollectionView.backgroundColor = lineGray;
    
//    self.collectionViewH.constant = (RecomGoodsLayoutH + 13)  * 2;
    
}

- (void)setModel:(MarketHomeModel *)model{
    
    _model = model;
    
    [self.myCollectionView reloadData];
    
    if (self.model.tuijian.count % 2 == 0) {
        
        self.collectionViewH.constant =  self.model.tuijian.count / 2   * (RecomGoodsLayoutH + 3);
        
    } else {
        
        self.collectionViewH.constant =  (self.model.tuijian.count + 1) / 2 * (RecomGoodsLayoutH + 3);
        
    }
    
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if (self.model == nil) {
        return 0;
    } else {
        
        return 1;
        
//        if (self.model.tuijian.count % 2 == 0) {
//
//            return self.model.tuijian.count / 2 ;
//
//        } else {
//
//            return (self.model.tuijian.count + 1) / 2 ;
//
//        }
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    if (self.model == nil) {
        return 0;
    } else {
        return self.model.tuijian.count;
    }
    
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ForYouRecomdSubCell *temp = (ForYouRecomdSubCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[ForYouRecomdSubCell identifierStr] forIndexPath:indexPath];
    
    temp.subModel = self.model.tuijian[indexPath.item];
    
    return temp;
    
}





-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                            withReuseIdentifier:@"CollectionViewFooterView"
                                                                                   forIndexPath:indexPath];
        
        return view;
        
        
      
    } else {
        

        ForYouRecommendHeaer *view = (ForYouRecommendHeaer *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                withReuseIdentifier:[ForYouRecommendHeaer identifierStr]
                                                                                                       forIndexPath:indexPath];
        
        return view;
        
    }
    
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GoodDetailController *tempVC = [[GoodDetailController alloc]init];
    
    MarketHSubModel *subModel = self.model.tuijian[indexPath.item];

    OneGoodsModel *tempModel = [[OneGoodsModel alloc]init];
    
    tempModel.goods_id = subModel.goods_id;
    
    tempModel.goods_image_url = subModel.pic_cover_small;
    
    tempVC.goodsModel = tempModel;
    
    [self.viewController.navigationController pushViewController:tempVC animated:YES];
    
    
    
    
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KScreenW, 0);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(KScreenW, 13);
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
